package com.example.parkdemo.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.EncryptUtils
import com.blankj.utilcode.util.SPUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.base.user.UserBean
import com.example.parkdemo.MainActivity
import com.example.parkdemo.R
import com.example.parkdemo.databinding.ActivityLoginBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import kotlin.math.log

@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {
    override fun initData() {
        vm.loginSuccess.observe(this) {
            val userBean = Gson().fromJson(it, UserBean::class.java)
            SPUtils.getInstance().put(Const.PARAM_USERNAME, userBean.uName)
            SPUtils.getInstance().put(Const.PARAM_PASSWORD, userBean.uPwd)
            SPUtils.getInstance().put(Const.PARAM_TOKEN, userBean.token)
            SPUtils.getInstance().put(Const.PARAM_UID, userBean.uId)
            vm.insertUserInfo(userBean)
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
        vm.inserUserInfoSuccess.observe(this) {
            Log.i("TagA", "添加数据库：$it")
        }
    }

    override fun initView() {
        binding.loginHeader.headerBack.visibility = View.GONE
        binding.loginHeader.headerTitle.text = "登录"
        binding.loginBtn.setOnClickListener {
            val username = binding.loginUsernameEt.text.toString()
            val password = binding.loginPasswordEt.text.toString()
            val timeString = (System.currentTimeMillis() / 1000).toString()

            val str = "tamboo"
            val sb = StringBuilder()
            sb.append(username).append(password).append(timeString).append(str)
            val sign = EncryptUtils.encryptMD5ToString(sb.toString()).toLowerCase()
            Log.i("TagA", "sign:$sign")

            val map = mutableMapOf<String, String>()
            map[Const.PARAM_USERNAME] = username
            map[Const.PARAM_PASSWORD] = password
            map[Const.PARAM_TIME] = timeString
            map[Const.PARAM_SIGN] = sign

            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
            vm.login(body)
        }
        val username = SPUtils.getInstance().getString(Const.PARAM_USERNAME)
        val password = SPUtils.getInstance().getString(Const.PARAM_PASSWORD)
        if (username.isNotEmpty() && password.isNotEmpty()) {
            val timeString = (System.currentTimeMillis() / 1000).toString()
            val str = "tamboo"
            val sb = java.lang.StringBuilder()
            sb.append(username).append(password).append(timeString).append(str)
            val sign = EncryptUtils.encryptMD5ToString(sb.toString()).toLowerCase()
            Log.i("TagA", "sign:$sign")

            val map = mutableMapOf<String, String>()
            map[Const.PARAM_USERNAME] = username
            map[Const.PARAM_PASSWORD] = password
            map[Const.PARAM_TIME] = timeString
            map[Const.PARAM_SIGN] = sign

            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
            vm.login(body)
        }


    }
}

